/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/13 00:59
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

(function () {

    "use strict";


    var api = {
        "getListsUrl": '/stream/group/getGroupLists',
        "addUrl": '/stream/group/addGroup',
        "editUrl": '/stream/group/editGroup',
        "deleteUrl": '/stream/group/deleteGroup'
    };

    sm.controller('admin-stream-group-controller', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.groupLists = [];
        $scope.addGroupName = '';
        $scope.editGroupName = '';
        $scope.edit = {
            index: 0,
            id: 0,
            groupName: ''
        };

        (function () {
            $http.get(api.getListsUrl).success(function (res) {
                if (res.state) {
                    $scope.groupLists = res.data;
                }
            });
        })();

        $scope.addGroup = function () {
            if (!$scope.addGroupName) {
                Modal.alert('请填写分组名称');
                return false;
            }

            $http.post(api.addUrl, {
                groupName: $scope.addGroupName
            }).success(function (res) {
                if (res.state) {
                    $scope.groupLists.unshift(res.data);
                } else {
                    var err = res.error ? res.error : '添加分组失败';
                    Modal.error(err);
                }
                $scope.addGroupName = '';
            });
        };

        $scope.editName = function ($index, id) {
            if (!id) {
                return false;
            }
            var groupName = $scope.groupLists[$index]['groupName'];
            if (groupName == '默认') {
                return false;
            }
            $('#editModal').modal();
            $scope.edit = {
                index: $index,
                id: id,
                groupName: groupName
            };
            $scope.editGroupName = groupName;
        };

        $scope.doEditGroupName = function () {
            if ($scope.edit.groupName == $scope.editGroupName) {
                return false;
            }

            if (!$scope.edit.id) {
                return false;
            }

            $http.post(api.editUrl, {
                groupId: $scope.edit.id,
                groupName: $scope.edit.groupName
            }).success(function (res) {
                $('#editModal').modal('hide');
                if (res.state) {
                    $scope.groupLists[$scope.edit.index]['groupName'] = $scope.edit.groupName;
                    $scope.editGroupName = '';
                } else {
                    var err = !res.error ? res.error : '编辑失败';
                    Modal.error(err);
                }
            });
        }

        $scope.delete = function ($index, id) {
            var l = $scope.groupLists[$index];
            if (id == 1) {
                Modal.alert({
                    'title': '无法删除',
                    'content': '默认分组,请勿删除'
                });
                return false;
            }
            if (l['deviceNum'] > 0) {
                Modal.alert({
                    'title': '无法删除',
                    'content': '该分组下包含设备, 请先将设备转至其它分组'
                });
                return false;
            }

            Modal.alert({
                'title': "您确认删除分组吗?",
                'content': "删除后不可恢复,请谨慎操作",
                'buttons': [{
                    'name': '取消'
                }, {
                    'name': '确定删除',
                    'event': function () {
                        doDelete($index, id);
                    }
                }]
            });

        };

        function doDelete($index, id) {
            $http.get(api.deleteUrl + '?groupId=' + id).success(function (res) {
                if (res.state) {
                    $scope.groupLists.splice($index, 1);
                } else {
                    var err = res.error ? res.error : '删除失败';
                    Modal.error(err);
                }
            });
        }

    }]);


})();